﻿using System;
using System.Diagnostics;
using System.Net;
using System.Xml.XPath;
using EveKillboard.Core;
using EveKillboard.Core.Services;
using EveKillboard.Data;

namespace AllianceListUpdater
{
    class Program
    {
        static void Main(string[] args)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());
            Load();
        }

        static void Load()
        {
            var request = WebRequest.Create("https://api.eveonline.com/eve/AllianceList.xml.aspx") as HttpWebRequest;
            if(request ==null) return;
            var response = request.GetResponse() as HttpWebResponse;
            if(response == null) return;

            int allyCount = 0;
            int corpCount = 0;

            var st = response.GetResponseStream();
            if(st==null) return;

            var dap = new DataAccessProvider();
            var api = new EveApiServicesProvider();
            
            var nav = new XPathDocument(st).CreateNavigator();
            var alliances = nav.Select("/eveapi/result/rowset/row");
            while(alliances.MoveNext())
            {
                var alliance = alliances.Current;
                if(alliance ==null) continue;
                var name = alliance.GetAttribute("name", "");
                var eveId = int.Parse(alliance.GetAttribute("allianceID", ""));
                if(string.IsNullOrEmpty(name) || eveId == 0) continue;

                var ally = dap.Find<Alliance>(a => a.Name == name ) ?? new Alliance() { Name = name, EveId = eveId };
                if (ally.EveId.GetValueOrDefault(0) == 0)
                    ally.EveId = int.Parse(alliance.GetAttribute("allianceID", ""));

                ally = ally.Id == 0 ? dap.Add(ally) : dap.Save(ally);
                allyCount++;

                var corps = alliance.Select("./rowset/row");
                while (corps.MoveNext())
                {
                    var corp = corps.Current;
                    if(corp ==null) continue;

                    var corpEveId = int.Parse(corp.GetAttribute("corporationID", ""));
                    if(corpEveId == 0) continue;

                    var corpInfos = api.GetCorporationInfos(corpEveId);
                    if(corpInfos==null) continue;

                    var corporation = dap.Find<Corporation>(c => c.Name == corpInfos.CorporationName) ?? new Corporation() { Name = corpInfos.CorporationName, EveId = corpInfos.CorporationId, LastKnownAlliance = ally };
                    if(corporation.EveId.GetValueOrDefault(0) == 0)
                        corporation.EveId = corpInfos.AllianceId;
                    if(corporation.LastKnownAlliance != ally )
                    {
                        corporation.LastKnownAlliance = ally;
                        corporation.Updated = DateTime.UtcNow;
                    }
                    corporation = corporation.Id == 0 ? dap.Add(corporation) : dap.Save(corporation);
                    corpCount++;
                }
            }

            st.Dispose();
            st = null;
            Trace.WriteLine(string.Format("{0} alliances updated or inserted", allyCount));
            Trace.WriteLine(string.Format("{0} corporations updated or inserted", corpCount));
        }
    }
}
